package com.fjwt.gz.mch.ctrl.merchant;

import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.MchInfoEntity;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.pay.mch.mchinfo.MchInfoSyncService;
import com.fjwt.gz.service.impl.MchInfoService;
import com.fjwt.gz.service.impl.SysUserService;
import com.fjwt.gz.zhyx.sms.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 主页数据类
 *
 * @author pangxiaoyu
 * @site https://pt.jdyd.xyz/
 * @date 2021-04-27 15:50
 */
@Slf4j
@RestController
@RequestMapping("/api/mainChart")
public class MainChartController extends CommonCtrl {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MchInfoService mchInfoService;
    @Autowired
    private MchInfoSyncService mchInfoSyncServicel;

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 商户基本信息、用户基本信息
     */
//    @PreAuthorize("hasAuthority('ENT_MCH_INFO')")
//    @RequestMapping(value = "", method = RequestMethod.GET)
//    public ApiRes userDetail() {
//        SysUser sysUser = sysUserService.getById(getCurrentUser().getSysUser().getSysUserId());
//        MchInfoEntity mchInfo = mchInfoService.getById(getCurrentMchNo());
//        JSONObject json = (JSONObject) JSON.toJSON(mchInfo);
//        json.put("loginUsername", sysUser.getLoginUsername());
//        json.put("realname", sysUser.getRealname());
//        return ApiRes.ok(json);
//    }

//    /**
//     * 发送手机号验证码
//     * @return
//     */
//    @PostMapping("/sendSmsCode")
//    public ApiRes sendSmsCode(){
//        String phone = getValString("iLoginUsername");
//        if (StringUtils.isEmpty(phone)){
//            throw new BizException("请输入手机号");
//        }
//        String smsType = getSmsPlatformSetting();
//        ISendSmsMsgService sendSmsMsgService = SpringBeansUtil.getBean(smsType + "SmsService", ISendSmsMsgService.class);
//        JSONObject jsonObject = sendSmsMsgService.sendSmsMsessage(phone,genSmsCode(),3);
//        Boolean aBoolean = sendSmsMsgService.sendMsg(jsonObject);
//        return ApiRes.ok(aBoolean);
//    }
//    public JSONObject  sendSmsMsessage(String phone, String content,String smsType) {
//        // 生成短信验证码
//        String verifyCode = genSmsCode();
//        // 将短信验证码存在redis中
//        JSONObject codeObj = new JSONObject();
//        codeObj.put("phone", phone);
//        codeObj.put("code", verifyCode);
//        codeObj.put("smsType", smsType);
//
//        logger.info("手机号:{},短信验证码:{}", phone, verifyCode);
//        content = String.format(content,verifyCode);
//        codeObj.put("content", content);
//        // 短信验证码缓存时间: 5分钟
//        RedisUtil.setString(CS.getCacheMchAuthSmsCode(phone), codeObj.toJSONString(), 60*5);
//        codeObj.remove("code");
//        return  codeObj;
//    }

//    /**
//     * iLoginUsername手机号
//     * ip密码
//     */
//    /**认证C端消费者的商户身份**/
//    @PostMapping("/checkFansIsMch")
//    public ApiRes checkFansIsMch(){
//        JSONObject jsonObject = new JSONObject();
//        String iLoginUsername = getValString("iLoginUsername");
//        String verCode = getValString("vc");
//        jsonObject.put("iLoginUsername",iLoginUsername);
//        String iP = getValString("ip");
//        jsonObject.put("iP",iP);
//        String mchNo = getValString("mchNo");
//        jsonObject.put("mchNo",mchNo);
//        if(StringUtils.isEmpty(iLoginUsername) || StringUtils.isEmpty(verCode) || StringUtils.isEmpty(mchNo)){
//            return ApiRes.customFail("请求参数缺失！");
//        }
//        JSONObject smsCode = SmsUtil.getSmsCOde(CS.SYS_ROLE_TYPE.FANS, iLoginUsername, 3);
//        if(null == smsCode){
//            throw new BizException("验证码已过期！");
//        }
//        String smsCodeRedis = smsCode.getString("code");
//        if (!smsCodeRedis.equals(verCode)){
//            throw new BizException("验证码错误");
//        }
//        LambdaQueryWrapper<MchInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(MchInfoEntity::getMchNo,mchNo);
//        MchInfoEntity mchInfo = mchInfoService.getBaseMapper().selectOne(queryWrapper);
//        if(null == mchInfo){
//            throw new BizException("该商户不存在！");
//        }
//        if(!iLoginUsername.equals(mchInfo.getPhone())){
//            String str = "您输入的和预留的手机号不一致";
//            JSONObject jsonResp = new JSONObject();
//            jsonResp.put("msg",str);
//            String reservePhone = DesensitizedUtil.mobilePhone(mchInfo.getPhone());
//            jsonResp.put("reservePhone",reservePhone);
//            return ApiRes.ok(jsonResp);
//        }
//        if(null != mchInfo.getOssRecordId()){
//            LambdaQueryWrapper<MchInfoEntity> queryWrapperOne = new LambdaQueryWrapper<>();
//            queryWrapperOne.eq(MchInfoEntity::getMchNo,mchNo);
//            queryWrapperOne.eq(MchInfoEntity::getPhone,iLoginUsername);
//            MchInfoEntity mchInfoEntity = mchInfoService.getBaseMapper().selectOne(queryWrapper);
//            if(null == mchInfoEntity){
//                throw new BizException("该商户不存在或手机号错误！");
//            }else{
//                return ApiRes.ok("true");
//            }
//        }else{
//            if(StringUtils.isEmpty(iP)){
//                throw new BizException("请输入密码！");
//            }
//        }
//        String aBoolean = mchInfoSyncServicel.checkFansIsMch(jsonObject);
//        JSONObject jsonResp = new JSONObject();
//        if (StringUtils.isEmpty(aBoolean)){
//            jsonResp.put("isMch",false);
//        }else{
//            jsonResp.put("isMch",aBoolean);
//        }
//        if("true".equals(aBoolean)){
//            return ApiRes.ok("true");
//        }else{
//            throw new BizException("商户号或者密码错误！");
//        }
//    }
}
